//
//  SecondViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/5/25.
//  Copyright (c) 2015年 xxx. All rights reserved.
//

#import "SecondViewController.h"
#import "BKArchinveViewModel.h"
#import "LoginViewController.h"
#import "BKArchiveTableViewCell.h"
#import "BKArchiveTableViewCell.h"
#import "BKArchiveThirdViewController.h"
#import <MBProgressHUD.h>
@interface SecondViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *colleageTableView;
@property (weak, nonatomic) IBOutlet UIImageView *cloudImageView;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _colleageTableView.backgroundColor = [UIColor clearColor];
    _colleageTableView.separatorColor = [UIColor clearColor];
    [_colleageTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
    CABasicAnimation *cloudAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    cloudAnimation.toValue = [NSNumber numberWithFloat:-300];
    
    cloudAnimation.duration = 30;
    cloudAnimation.repeatCount = 1000000;
    [_cloudImageView.layer addAnimation:cloudAnimation forKey:@"transform.translation.x"];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)p_back:(id)sender {
    [_cloudImageView removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *string = @"BKCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        [tableView registerClass:[BKArchiveTableViewCell class] forCellReuseIdentifier:string];
        cell = [tableView dequeueReusableCellWithIdentifier:string];
    }
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = [_dataArray[indexPath.row] objectForKey:@"name"];
    cell.textLabel.font = [UIFont fontWithName:@"DFWaWaW5-GB" size:18];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [_dataArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 20)];
    return footerView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 20;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (scrollView.contentOffset.y > ((scrollView.contentSize.height - scrollView.frame.size.height))) {
        
        __weak SecondViewController *weakSelf = self;
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:_params];
        NSInteger currentPage = [[dic objectForKey:@"page"] integerValue];
        dic[@"page"] = @(currentPage+1);
        MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
        hud.labelText = @"正在提交";
        
        [hud show:YES];
        [[BKArchinveViewModel share] searchWithParams:dic
                                      success:^(NSArray *dataArray) {
                                          [hud hide:YES];
                                          weakSelf.params = dic;
                                          weakSelf.dataArray = dataArray;
                                          [weakSelf performSelectorOnMainThread:@selector(p_reloadData) withObject:nil waitUntilDone:YES];
                                          
        }
                                      failure:^(NSString *message) {
                                          [hud hide:YES];
                                          UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                                          [alertView show];
            
        }];
        
    }
    
}

- (void)p_reloadData{

    [_colleageTableView reloadData];
}
- (IBAction)setMeng:(id)sender {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if ([userDefaults objectForKey:@"Cookie"] !=nil) {
        NSIndexPath *indexPath = [_colleageTableView indexPathForSelectedRow];
        
        
        
        [[BKArchinveViewModel share] submitWithCid:indexPath.row
                                           success:^(NSString *colleage) {
            
                                               dispatch_queue_t mainQueue = dispatch_get_main_queue();
                                               dispatch_async(mainQueue, ^(void) {
                                                   BKArchiveThirdViewController *thirdViewController = [[BKArchiveThirdViewController alloc] initWithNibName:NSStringFromClass([BKArchiveThirdViewController class]) bundle:nil];
                                                   thirdViewController.colleage = colleage;
                                                   
                                                   [self.navigationController pushViewController:thirdViewController animated:YES];
                                               });

                                               
        }
                                           failure:^(NSString *message) {
                                               dispatch_queue_t mainQueue = dispatch_get_main_queue();
                                               dispatch_async(mainQueue, ^(void) {
                                                   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                                                   [alertView show];
                                               });

            
        }];
    }else{
    
        LoginViewController *login = [[LoginViewController alloc] initWithNibName:NSStringFromClass([LoginViewController class]) bundle:nil];
        [self presentViewController:login animated:YES completion:nil];
        
    }
}

@end
